/// <reference path="jquery-vsdoc.js" />
//*************************************************************************************
// File     : braingnat.js
// Version  : 0.4.1
// Requires : jquery.js (version 1.3.2+)
// Author   : Kyle Weems (ksw)
// Origin   : mindfly.com
// Created  : Jul 29, 2008
// Modified : Mar 10, 2010
// Purpose  : BrainGnat - A collection of handy solutions and functions for problems and 
//            desired functionality for client websites by Mindfly.
//*************************************************************************************

/* List of methods */

// 0.0.1
// setContentHeight 	- Adjusts height of page content to make it fit in browser.
// 0.0.5
// stickyList			- Gives list items the class 'stuck' when they are hovered over and in lists with the '.stickyList' class.
// 0.1.0
// slideshow.fadein  	- Loops a series of images in a slideshow.
// slideshow.ajaxLoadCrossfade 	- Ajax call to a document that lists the images for use in a slideshow
// 0.1.4
// randomBackgroundImage- Add a randomly selected background image from a provided array to the provided element.
// randomImage          - Set the source of the provided image tag to the a randomly selecte image from the provided array.
// 0.1.5
// html5Shiv            - Fixes IE to support CSS styling of HTML5 elements
// 0.2.0
// setTabs              - Adds tab functionality to given section where tabs have class 'tab' and content of tabs has class 'tabbedContent'
// 0.3.0
// Google.map functions added. Moved to an extension script in 0.4.0
// 0.3.1
// Google.map functions upgraded. Moved to an extension script in 0.4.0
// 0.3.2
// Google.map functions upgraded. Moved to an extension script in 0.4.0
// 0.3.3
// Updated slideshow.ajaxLoadCrossfade as follows:
// slideshow.ajaxLoadCrossfade(file as string, wrapper as string, frontElem as string, slideDuration as integer, fadeSpeed as integer, random as boolean)
// - file is the name of a file that is holding the list of images to rotate
// - wrapper is the CSS selector of the element that holds the image that the slideshow takes place on
// - frontElem is the CSS selector of the image (in relation to the wrapper) that the slideshow takes place on. Default is "img"
// - slideDuration is the time in milliseconds between slides. Default is 5000 ms.
// - fadeSpeed is the time the transition takes. Default is 1000 ms.
// - random determines whether the images rotate in the order loaded, or in random order. Default is false.
// 0.4.0
// BrainGnat.Google has been spun out into a separate script that must be included in projects that make use of it.
// Syntax is the same, it merely exists as an extension now.
// 0.4.1
// BrainGnat.checkForLocal
var BrainGnat = function() {
    return {
        version: "0.4.1",
        test: function() {
            alert('BrainGnat version ' + BrainGnat.version + ' is loading correctly.');
        },
        setContentHeight: function(verticalOffset) {
            if (!verticalOffset) {
                verticalOffset = 0;
            }
            var minContentHeight = $(window).height() - ($('#branding').height() + $('#site_info').height() + verticalOffset);
            if ($('#content').height() < minContentHeight) {
                $('#content').height(minContentHeight);
            }
        },
        slideshow: {
            fadein: function(elem, imageList, slideDuration, fadeSpeed, current, frontElem) {
                var listSize = imageList.length;
                if (!current || current >= listSize) current = 0;
                if (!frontElem) frontElem = "img";
                if (!slideDuration) slideDuration = 5000;
                if (!fadeSpeed) fadeSpeed = 1000;
                if (frontElem == "img") {
                    $(elem + " " + frontElem).attr("src", imageList[current]);
                } else {
                    $(elem + " " + frontElem).css("background-image", "url(" + imageList[current] + ")");
                }
                if (current >= (listSize - 1)) {
                    $(elem).css("background", "transparent url(" + imageList[0] + ") no-repeat");
                } else {
                    $(elem).css("background", "transparent url(" + imageList[current + 1] + ") no-repeat");
                }
                $(elem + " " + frontElem).animate({ opacity: "1" }, slideDuration).animate({ opacity: "0.01" }, fadeSpeed, function() { $(this).css("opacity", "1"); BrainGnat.slideshow.fadein(elem, imageList, slideDuration, fadeSpeed, current + 1, frontElem) });
            },
            ajaxLoadCrossfade: function(file, wrapper, frontElem, slideDuration, fadeSpeed, random) {
                // This function makes use of the .NET files: imageList.aspx, imageList.aspx.vb which can be found in proofs.
                if (!slideDuration) slideDuration = 5000;
                if (!fadeSpeed) fadeSpeed = 1000;
                if (!frontElem) frontElem = "img";
                if (!random || random == null) random = false;
                $.get(file, function(data) {
                    var imageList = data.split("|");
                    imageList.pop();
                    if (random == false) {
                        BrainGnat.slideshow.fadein(wrapper, imageList, slideDuration, fadeSpeed, 0, frontElem);
                    } else {
                        var newList = new Array();
                        var j = 0;
                        while (imageList.length > 0) {
                            var i = Math.floor(Math.random() * imageList.length);
                            newList[j] = imageList[i];
                            j++;
                            if (imageList.length > 1) {
                                for (k = i; k < (imageList.length - 1); k++) {
                                    imageList[k] = imageList[k + 1];
                                }
                            }
                            imageList.pop();
                        }
                        BrainGnat.slideshow.fadein(wrapper, newList, slideDuration, fadeSpeed, 0, frontElem);
                    }
                });

            }
        },
        stickyList: function() {
            $('.stickyList > li').bind('mouseover', function() {
                $('.stuck').removeClass('stuck');
                if ($(this).children('ul').children().length > 0) {
                    $(this).addClass('stuck');
                }
            });
            $('.stickyList li ul').bind('mouseout', function() {
                $(this).parent().removeClass('stuck');
            });
        },
        randomBackgroundImage: function(elem, imageList) {
            var i = Math.floor(Math.random() * imageList.length);
            $(elem).css({ 'background-image': 'url(' + imageList[i] + ')' });
        },
        randomImage: function(elem, imageList) {
            var i = Math.floor(Math.random() * imageList.length);
            $(elem).attr('src', imageList[i]);
        },
        html5Shiv: function() {
            var shivstring = "section|article|aside|header|footer|nav|dialog|figure|audio|video|embed|m|meter|time|canvas|command|datagrid|details|datalist|datatemplate|rule|nest|event-source|output|progress";
            var shiv = shivstring.split('|');
            for (i = 0; i < shiv.length; i++) {
                document.createElement(shiv[i]);
            }
        },
        setTabs: function(elem) {
            $(elem + ' .tab').bind('click', function() {
                $(elem + ' .tab').removeClass('active');
                $(elem + ' .tabbedContent').removeClass('show');
                var tabClasses = $(this).attr('class').split(' ');
                for (i = 0; i < tabClasses.length; i++) {
                    if (tabClasses[i] != 'tab') {
                        $(elem + ' .tabbedContent.' + tabClasses[i]).addClass('show');
                    }
                }
                $(this).addClass('active');
            });
        },
        checkForLocal: function() {
            if (location.hostname == "localhost") {
                var p = new Array();
                p = location.pathname.split("/");
                for (i = 0; i < $('img').length; i++) {
                    var s = $('img:eq(' + i + ')').attr('src');
                    if (s.substr(0, 1) == '/') {
                        $('img:eq(' + i + ')').attr('src', '/' + p[1] + s);
                    }
                }
            }
        }
    }
} ();


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Pre-Singleton Functions for pre-0.1.0 Backwards Compatibility 
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function setContentHeight(verticalOffset) {
    if (!verticalOffset) {
        verticalOffset = 0;
    }
    var minContentHeight = $(window).height() - ($('#branding').height() + $('#site_info').height() + verticalOffset);
    if ($('#content').height() < minContentHeight) {
        $('#content').height(minContentHeight);
    }
}

function fadeinSlideshow(elem, imageList, slideDuration, fadeSpeed, current) {
    var listSize = imageList.length;
    if (!current || current >= listSize) current = 0;
    if (!slideDuration) slideDuration = 5000;
    if (!fadeSpeed) fadeSpeed = 1000;
    $(elem + " img").attr("src", imageList[current]);
    if (current >= (listSize - 1)) {
        $(elem).css("background", "transparent url(" + imageList[0] + ") no-repeat");
    } else {
        $(elem).css("background", "transparent url(" + imageList[current + 1] + ") no-repeat");
    }
    $(elem + " img").animate({ opacity: "1" }, slideDuration).animate({ opacity: "0.01" }, fadeSpeed, function() { $(this).css("opacity", "1"); fadeinSlideshow(elem, imageList, slideDuration, fadeSpeed, current + 1) });
}

function ajaxLoadCrossfade(file, wrapper, slideDuration, fadeSpeed) {
    if (!slideDuration) slideDuration = 5000;
    if (!fadeSpeed) fadeSpeed = 1000;
    $.get(file, function(data) { fadeinSlideshow(wrapper, data.split(" "), slideDuration, fadeSpeed); });
}

function stickyList() {
    $('.stickyList > li').bind('mouseover', function() {
        $('.stuck').removeClass('stuck');
        if ($(this).children('ul').children().length > 0) {
            $(this).addClass('stuck');
        }
    });
}